package edu.zut.cs.software.perfect.teacher.service;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import edu.zut.cs.software.perfect.base.service.GenericGenerator;
import edu.zut.cs.software.perfect.teacher.domain.Department;

public class TeacherEntityGenerator extends GenericGenerator {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LogManager.getLogger(TeacherEntityGenerator.class.getName());

		List<Department> departmentList;
		DepartmentManager departmentManager;
		
		@Autowired
		public void setDepartmentManager(DepartmentManager departmentManager) {
			this.departmentManager = departmentManager;
		}
		@Before
		public void setUp() throws Exception{
			this.departmentList=new ArrayList<Department>();
			InputStream input=DepartmentManagerTest.class.getResourceAsStream("/Department.xlsx");
			@SuppressWarnings("resource")
			XSSFWorkbook wb = new XSSFWorkbook(input);
			XSSFSheet sheet = wb.getSheetAt(0);
			DataFormatter dataFormatter = new DataFormatter();
			for (int i = 1; i <= sheet.getLastRowNum(); i++) {
				Row row = sheet.getRow(i);
				Department s=new Department();
				for (int j = 0; j < row.getLastCellNum(); j++) {
					Cell cell = row.getCell(j);
					if (cell != null) {
						String value = dataFormatter.formatCellValue(cell);
						if (j == 1)
							s.setDepartmentName(value);
						if (j == 2)
							s.setDepartmentCode(value);			
					}
			}
				this.departmentList.add(s);
		}
		}
			@Test
			public void test() {
				this.departmentManager.deleteAll();
				this.departmentManager.save(this.departmentList);
				Iterable<Department> result = this.departmentManager.findAll();
				if (logger.isInfoEnabled()) {
					logger.info("test() - Iterable<Department> result={}", result); //$NON-NLS-1$
				}
				
				
				
				
				
			}
}
